﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IBuy.Entity;
using System.Collections;

namespace IBuy.Forms
{
    public partial class GoodsTypesForm : Form
    {
        private GoodType type;

        public GoodsTypesForm()
        {
            InitializeComponent();
            type = new GoodType();
        }

        private void GoodsTypesForm_Load(object sender, EventArgs e)
        {
            SetDataSource();
        }

        private void SetDataSource()
        {
            DataSet dataset = type.GetAllType();
            this.dgvGoodsType.DataSource =dataset.Tables[0].DefaultView;
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            //判断该类型是否已存在
            if (type.GetTypeIDByName(this.txtTypeName.Text.Trim()) != null)
            {
                MessageBox.Show("该类型已存在！");
            }
            else
            {
                Hashtable table = new Hashtable();
                table.Add("TYPENAME", this.txtTypeName.Text.Trim());
                if (type.InsertGoodsType(table))
                {
                    MessageBox.Show("添加商品类型成功！");
                }
                else
                {
                    MessageBox.Show("添加商品类型失败！");
                }

                SetDataSource();
            }
            Clear();
        }

        private void Clear()
        {
            this.txtTypeName.Text = "";
            this.labelID.Text = "空";
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            String ID = this.labelID.Text;
            if (!ID.Equals(""))
            {
                String where = " where TYPEID=" + ID;
                Hashtable table = new Hashtable();
                table.Add("TYPENAME", this.txtTypeName.Text);
                if (type.UpdateGoodsType(table, where))
                {
                    MessageBox.Show("更新类型成功！");
                }
                else
                {
                    MessageBox.Show("更新类型失败！");
                }
                Clear();
                SetDataSource();
            }
            else
            {
                MessageBox.Show("请确定要更改的种类编号不为空！");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            String Name = this.txtTypeName.Text;
            if (!Name.Equals(""))
            {
                if (type.DeleteGoodsType(Name))
                {
                    MessageBox.Show("删除类型成功！");
                }
                else
                {
                    MessageBox.Show("删除类型失败！");
                }
                Clear();
                SetDataSource();
            }
            else
            {
                MessageBox.Show("请确定要更改的种类编号不为空！");
            }
        }

        private void dgvGoodsType_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = this.dgvGoodsType.Rows[e.RowIndex];
            this.labelID.Text = row.Cells[0].Value.ToString();
            this.txtTypeName.Text = row.Cells[1].Value.ToString();
        }
    }
}
